# urlencode 的应用场景：多个参数的时候
# https://www.baidu.com/s?wd=周杰伦&sex=男
# import urllib.parse
#
# data = {
#     'wd': '周杰伦',
#     'sex': '男',
#     'city': '中国台湾'
# }
#
# a = urllib.parse.urlencode(data)
# print(a)

# 获取 https://www.baidu.com/s?wd=%E5%91%A8%E6%9D%B0%E4%BC%A6
import urllib.parse
import urllib.request

base_url = 'https://www.baidu.com/s'
data = {
    'wd': '周杰伦',
    'sex': '男',
    'city': '中国台湾'
}

new_data = urllib.parse.urlencode(data)
print(new_data)

# 请求路径
url = base_url + new_data

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0'
}

request = urllib.request.Request(url=url, headers=headers)

response = urllib.request.urlopen(request)

comtent = response.read().decode('utf-8')
print(comtent)